package org.come.good;

import com.tool.pet.PetProperty;
import com.tool.role.RoleData;
import com.tool.role.RoleProperty;
import org.come.Frame.ZhuFrame;
import org.come.entity.Goodstable;
import org.come.mouslisten.GoodsMouslisten;
import org.come.mouslisten.PetAddPointMouslisten;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 药瓶使用
 *
 * @author Administrator
 *
 */
public class Medicine {
	/**
	 * 传入药品属性 玩家平时加血加蓝
	 *
	 * @throws Exception
	 */
	public static void RedAndBlue(Goodstable goodPlace, String value){
		// 药的属性
		int[] yao = AnalysisString.yao(value);
		// 总的hp和当前的hp
		double hp_z, hp_d;
		// 总的mp和当前的mp
		double mp_z, mp_d;
		hp_z = RoleProperty.getHp(RoleData.getRoleData().getLoginResult());
		hp_d = RoleData.getRoleData().getLoginResult().getHp().intValue();
		mp_z = RoleProperty.getMp(RoleData.getRoleData().getLoginResult());
		mp_d = RoleData.getRoleData().getLoginResult().getMp().intValue();
		if (!isyao(yao, hp_z, hp_d, mp_z, mp_d)) {
            return;
        }
		if (yao[0] != 0 || yao[2] != 0) {
			hp_d = hp_d + yao[0] + (yao[2] * hp_z) / 100;
			if (hp_d > hp_z) {
				hp_d = hp_z;
			}
			RoleData.getRoleData().getLoginResult().setHp(new BigDecimal(hp_d));
		}
		if (yao[1] != 0 || yao[3] != 0) {
			mp_d = mp_d + yao[1] + (yao[3] * mp_z) / 100;
			if (mp_d > mp_z) {
				mp_d = mp_z;
			}
			RoleData.getRoleData().getLoginResult().setMp(new BigDecimal(mp_d));
		}
		goodPlace.setUsetime(goodPlace.getUsetime() - 1);
//		Music.addyinxiao("战斗、站立、行走使用药品.mp3");//药品使用监听
	}

	/**
	 * 召唤兽
	 */
	public static void PetRedAndBlue(Goodstable goodPlace, String value){
		// 药的属性
		int[] yao = AnalysisString.yao(value);
		// 总的hp和当前的hp
		double hp_z, hp_d;
		// 总的mp和当前的mp
		double mp_z, mp_d;
		int[] pets=PetProperty.getPetHMASp(UserMessUntil.getChosePetMes());
		hp_z = pets[0];
		hp_d = UserMessUntil.getChosePetMes().getBasishp();
		mp_z = pets[1];
		mp_d = UserMessUntil.getChosePetMes().getBasismp();
		if (!isyao(yao, hp_z, hp_d, mp_z, mp_d)){
			return;
		}
		if (yao[0] != 0 || yao[2] != 0) {
			hp_d = hp_d + yao[0] + (yao[2] * hp_z) / 100;
			if (hp_d > hp_z) {
				hp_d = hp_z;
			}
			UserMessUntil.getChosePetMes().setBasishp((int) hp_d);
		}
		if (yao[1] != 0 || yao[3] != 0) {
			mp_d = mp_d + yao[1] + (yao[3] * mp_z) / 100;
			if (mp_d > mp_z) {
				mp_d = mp_z;
			}
			UserMessUntil.getChosePetMes().setBasismp((int) mp_d);
		}
		goodPlace.setUsetime(goodPlace.getUsetime() - 1);
	}
	/**
	 * 判断能否用药
	 *
	 * @param gz
	 * @param bz
	 */
	public static boolean isyao(int[] yao, double hp_z, double hp_d,double mp_z, double mp_d) {
		if (hp_z == hp_d && mp_z == mp_d) {
			ZhuFrame.getZhuJpanel().addPrompt2("你已经很健康了");
			return false;
		}
		boolean is = false;
		if ((yao[0] != 0 || yao[2] != 0) && hp_z > hp_d) {
			is = true;
			// 吃药效果
			Music.addyinxiao("战斗、站立、行走使用药品.mp3");
		} else {
			if (hp_z <= hp_d && (yao[0] != 0 || yao[2] != 0)) {
				ZhuFrame.getZhuJpanel().addPrompt2("生命值已满");
			}
		}
		if ((yao[1] != 0 || yao[3] != 0) && mp_z > mp_d) {
			is = true;
			// 吃药效果
			Music.addyinxiao("战斗、站立、行走使用药品.mp3");
		} else {
			if (mp_z <= mp_d && (yao[1] != 0 || yao[3] != 0)) {
				ZhuFrame.getZhuJpanel().addPrompt2("魔法值已满");
			}
		}
		return is;
	}
	// 吃红药 第一个是固定第二个是百分比
	public static void Red(int gz, int bz) {
		double hp_z, hp_d;
		hp_z = RoleProperty.getHp(RoleData.getRoleData().getLoginResult());
		hp_d = RoleData.getRoleData().getLoginResult().getHp().intValue();
		hp_d = hp_d + gz + (bz * hp_z) / 100;
		if (hp_d > hp_z) {
			hp_d = hp_z;
		}
		RoleData.getRoleData().getLoginResult().setHp(new BigDecimal(hp_d));
		String mes = Agreement.getAgreement().rolechangeAgreement("H"+RoleData.getRoleData().getLoginResult().getHp()+"="+RoleData.getRoleData().getLoginResult().getMp());
		SendMessageUntil.toServer(mes);
		// 进行面板刷新
		PetAddPointMouslisten.getplayerValue();

	}

	// 吃蓝药
	public static void Blue(int gz, int bz) {
		double mp_z, mp_d;
		mp_z = RoleProperty.getMp(RoleData.getRoleData().getLoginResult());
		mp_d = RoleData.getRoleData().getLoginResult().getMp().intValue();
		mp_d = mp_d + gz + (bz * mp_z) / 100;
		if (mp_d > mp_z) {
			mp_d = mp_z;
		}
		RoleData.getRoleData().getLoginResult().setMp(new BigDecimal(mp_d));
		String mes = Agreement.getAgreement().rolechangeAgreement("H"+RoleData.getRoleData().getLoginResult().getHp()+"="+RoleData.getRoleData().getLoginResult().getMp());
		SendMessageUntil.toServer(mes);
		// 进行面板刷新
		PetAddPointMouslisten.getplayerValue();
	}

	/**
	 * 自动吃药 lx==0 吃红 1吃蓝
	 */
	public static void zdchiyao(int lx) {
 
		int z = 0;
		long d = 0;
		if (lx == 0) {
			z = RoleProperty.getHp(RoleData.getRoleData().getLoginResult());
			d = RoleData.getRoleData().getLoginResult().getHp().intValue();
		} else if (lx == 1) {
			z = RoleProperty.getMp(RoleData.getRoleData().getLoginResult());
			d = RoleData.getRoleData().getLoginResult().getMp().intValue();
		} else if (lx == 2) {
			if (UserMessUntil.getChosePetMes()==null) {return;}
			int[] pets=PetProperty.getPetHMASp(UserMessUntil.getChosePetMes());
			z = pets[0];
			d = UserMessUntil.getChosePetMes().getBasishp();
		} else if (lx == 3) {
			if (UserMessUntil.getChosePetMes()==null) {return;}
			int[] pets=PetProperty.getPetHMASp(UserMessUntil.getChosePetMes());
			z = pets[1];
			d = UserMessUntil.getChosePetMes().getBasismp();
		}
		List<Goodstable> goodstables = GoodsListFromServerUntil.chaxunss(0);
		for (int i = 0; i < goodstables.size(); i++) {
			Goodstable good=goodstables.get(i);
			int usetime=good.getUsetime();
			if (lx == 0 || lx == 2) {
				if (good.getValue().indexOf("HP") == -1) {
                    continue;
                }
			} else if (lx == 1 || lx == 3) {
				if (good.getValue().indexOf("MP") == -1) {
                    continue;
                }
			}
			if (d < z) {
				while (good.getUsetime() > 0 && d < z) {
					try {
						if (lx == 0 || lx == 1) {
							RedAndBlue(good, good.getValue());
							if (lx == 0) {
								d = RoleData.getRoleData().getLoginResult().getHp().intValue();
							} else if (lx == 1) {
								d = RoleData.getRoleData().getLoginResult().getMp().intValue();
							}
						} else {
							PetRedAndBlue(good, good.getValue());
							if (lx == 2) {
								d = UserMessUntil.getChosePetMes().getBasishp();
							} else if (lx == 3) {
								d = UserMessUntil.getChosePetMes().getBasismp();
							}
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				if (good.getUsetime()!=usetime) {
					GoodsMouslisten.gooduse(good,1);
					if (good.getUsetime() <= 0) {
                        GoodsListFromServerUntil.Deletebiaoid(good.getRgid());
                    }
					if (lx == 0 || lx == 1) {
						String mes = Agreement.getAgreement().rolechangeAgreement("H"+RoleData.getRoleData().getLoginResult().getHp()+"="+RoleData.getRoleData().getLoginResult().getMp());
						SendMessageUntil.toServer(mes);
						PetAddPointMouslisten.getplayerValue();
					} else {
						SendRoleAndRolesummingUntil.sendRoleSumming(UserMessUntil.getChosePetMes());
						PetAddPointMouslisten.showPetValue();
					}
					Music.addyinxiao("战斗、站立、行走使用药品.mp3");
				}
			}
		}
		if (d < z) {
			ZhuFrame.getZhuJpanel().addPrompt("你的药品不够了");
			return;
		}
//		//吃药效果
		 Music.addyinxiao("战斗、站立、行走使用药品.mp3");
	}


	public static void main(String[] args) {
		Music.addyinxiao("战斗、站立、行走使用药品.mp3");
	}
}
